<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\facade\Env;
use think\auth\Auth;
class Common extends Controller
{   
  public function initialize(){
      $model=request()->module();
      $con=request()->controller();
      $action=request()->action();
      $name=$model."/".$con."/".$action;//组合规则名称
      $auth = Auth::instance();
    //   if($auth->check($name,session('uid'))){// 第一个参数是规则名称,第二个参数是用户UID
    //     //有显示操作按钮的权限
    //     }else{
    //         //没有显示操作按钮的权限
    //     }
  }
    /**
     * 栏目图片上传
     *
     * @return void
     */
    public function uploadimg(){
        if (request()->isPost()) {
            $file = request()->file('picfile');
            if ($file) {
                $conf=json_decode(db('config')->find()['config'],true);//获取配置信
                $sy=Env::get('root_path')."public\uploads\\".$conf['water_img'];//水印图
            
                $info = $file->move('../public/uploads');
                if($info){
                    if ($conf['thumb']=='是') {//判断是否允许上传缩略图
                        //缩略图裁剪方式
                       switch ($conf['thumb_way']) {
                           case '等比例缩放':
                               $thumb_way=1;
                               break;
                           case '缩放后填充':
                               $thumb_way=2;
                               break;
                           case '居中裁剪':
                               $thumb_way=3;
                               break;
                           case '左上角裁剪':
                               $thumb_way=4;
                               break;
                           case '右下角裁剪':
                               $thumb_way=5;
                               break;
                           case '固定尺寸缩放':
                               $thumb_way=6;
                               break;
                           
                           default:
                               $thumb_way=1;
                               break;
                       }
                        //水印图位置
                       switch ($conf['water_pos']) {
                           case '左上角':
                               $thumb_pos=1;
                               break;
                           case '上居中':
                               $thumb_pos=2;
                               break;
                           case '右上角':
                               $thumb_pos=3;
                               break;
                           case '左居中':
                               $thumb_pos=4;
                               break;
                           case '居中':
                               $thumb_pos=5;
                               break;
                           case '右居中':
                               $thumb_pos=6;
                               break;
                           case '左下角':
                               $thumb_pos=7;
                               break;
                           case '下居中':
                               $thumb_pos=8;
                               break;
                           case '右下角':
                               $thumb_pos=9;
                               break;
                           default:
                               $thumb_pos=9;
                               break;
                       }
                       // 数据处理
                       $lit=Env::get('root_path')."public\\"."uploads\\".$info->getSaveName();
                       $image = \think\Image::open($lit);
                       if ($conf['water']=='是') {
                       $image->thumb($conf['thumb_width'],$conf['thumb_height'],$thumb_way)->water($sy,$thumb_pos,$conf['tmd'])->save($lit);
                       }else{
                        $image->thumb($conf['thumb_width'],$conf['thumb_height'],$thumb_way)->save($lit);
                       }
                       
                   }
                   $pic= "uploads\\".$info->getSaveName(); 
                }
            }
        }
        return json(['code'=>1,'msg'=>'上传成功','path'=>$pic]);
    }
     /**
     * 单张图片删除
     */
    public function delImg($url=""){
        $path=Env::get('root_path')."\\public\\". $url;
        if ($url!==""||!empty($url)) {
            if  (file_exists($path)) {
                $res=unlink($path);
                if ($res) {
                    return json(['code'=>1,'msg'=>'删除成功']);
                }
                return json(['code'=>0,'msg'=>'删除失败']);
            }
            return json(['code'=>2,'msg'=>'文件不存在']);
        }
        
    }
}
